﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ALDIOrganizer
{
	public static class IListDateTime
	{
        public static DateTime Closest(
            this IList<DateTime> pList,
            DateTime pSeeked)
        {
            var _differences = from _element in pList
                               select new
                               {
                                   Value = _element,
                                   Distance = ((_element - pSeeked) >= TimeSpan.Zero) ?
                                        _element - pSeeked :
                                        pSeeked - _element
                               };

            return (from _difference in _differences
                    orderby _difference.Distance ascending
                    select _difference.Value).First();                   
        }
	}
}
